package ecom.shell;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import shell.Shell;
import shell.command.ShellContext;
import ecom.ejb.CustomerRemote;
import ecom.ejb.AdminRemote;
import ecom.ejb.EuroConverterRemote;
import ecom.ejb.EuroConverterRemote.Devise;

/**
 * eCommerce Application Sample for J2EE Training. Shell de commande de
 * l'application.
 * @version 1.0
 */
public final class EcomShell extends Shell implements EcomShellConstantes {
    /**
     * Lancement du shell eCommerce.
     * 
     * @param args
     *            Argument de lancement
     */
    public static void main(final String args[]) {
	new EcomShell(args).run();
    }

    /**
     * Constructeur.
     * 
     * @param args
     *            Argument de lancement
     */
    public EcomShell(final String args[]) {
	super(args);
	ShellContext context = this.getContexte();

	// Remove or add commands

	// Add "output" command.
	addCommand(new ecom.shell.OutputCommandImpl(context));
	// Add "currency" commande
	addCommand(new ecom.shell.CurrencyCommandImpl(context));
//	// Add "language" command.
//	addCommand(new ecom.shell.LanguageCommandImpl(context));
	// Add "batch" command.
	addCommand(new ecom.shell.BatchCommandImpl(context));
  	// Add "account" command.
  	addCommand(new ecom.shell.AccountCommandImpl(context));
	// Add "store" command.
	addCommand(new ecom.shell.StoreCommandImpl(context));
  	// Add "product" command.
  	addCommand(new ecom.shell.ProductCommandImpl(context));
  	// Add "cart" command.
  	addCommand(new ecom.shell.CartCommandImpl(context));
	// Add "admin" command.
	addCommand(new ecom.shell.AdminCommandImpl(context));

	// put the EJBs' homes in the context
	try {
	    Context initial = new InitialContext();

	    context.setVar(EcomShellConstantes.CUSTOMER,
		    (CustomerRemote) initial
			    .lookup(CustomerRemote.class.getName()));
	    context.setVar(EcomShellConstantes.ADMIN, (AdminRemote) initial
		    .lookup(AdminRemote.class.getName()));
	    context.setVar(EcomShellConstantes.CONVERTER,
		    (EuroConverterRemote) initial
			    .lookup(EuroConverterRemote.class.getName()));
	} catch (final NamingException e) {
	    System.out.println("Erreur lors de l'instanciation des connexions");
	}

	// Variables par défaut
	context.setVar(EcomShellConstantes.OUTPUT_MIME_FORMAT,
		       EcomShellConstantes.TEXT_MIMETYPE);
	context.setVar(EcomShellConstantes.CURRENT_CURRENCY, Devise.EUR);
	context.setVar(EcomShellConstantes.CURRENT_LANG,
		       EcomShellConstantes.LANG_EN);
	context.setVar(EcomShellConstantes.CURRENT_TX, null);
	context.setVar(EcomShellConstantes.BANNER, getBanner());
	context.setVar(EcomShellConstantes.EST_ADMIN, EcomShellConstantes.NON);
    }

    /**
     * @see shell.Shell#getBanner()
     */
    @Override
    public String getBanner() {
	/*
	 * Variable de retour
	 */
	StringBuilder retour = new StringBuilder();

	if (this.getIn().equals(System.in)) {
	    retour.append("+------------------------------------------------+");
	    retour.append(System.getProperties().getProperty("line.separator"));
	    retour.append("! eCommerce Application Sample for J2EE Training !");
	    retour.append(System.getProperties().getProperty("line.separator"));
	    retour.append("! par Mike Attia                                 !");
	    retour.append(System.getProperties().getProperty("line.separator"));
	    retour.append("!     François Haurit                            !");
	    retour.append(System.getProperties().getProperty("line.separator"));
	    retour.append("!     Vincdent Jouglard                          !");
	    retour.append(System.getProperties().getProperty("line.separator"));
	    retour.append("!     Nicolas Roy                          v1.0  !");
	    retour.append(System.getProperties().getProperty("line.separator"));
	    retour.append("+------------------------------------------------+");
	}

	return retour.toString();
    }
}
